<?php
namespace Products\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;

class BuyTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function addBuying($userid,$product,$buyproductdetails)
    {
		if(isset($buyproductdetails['acprice']) && $buyproductdetails['acprice']!=""){
			$price=$buyproductdetails['acprice'];
		}else{
			$price="";
		}
		if(isset($buyproductdetails['quantity']) && $buyproductdetails['quantity']!=""){
			$quantity=$buyproductdetails['quantity'];
		}else{
			$quantity="";
		}
		$data = array(
			'category_id'        => $product->category_id,
			'product_id'         => $product->product_id, 	
			'user_id' 	         => $userid, 	
			'business_user_id' 	 => $product->user_id,			
			'order_for_quantity' => $quantity,	
			'order_for_price' 	 => $price,	
			'order_date_time' 	 => date('Y-m-d H:i:s'),	
			'order_status' 	     => 0,	
		);
		$this->tableGateway->insert($data);		
		return $this->tableGateway->lastInsertValue;
    }
	public function buylist($bid){
		$select = $this->tableGateway->getSql()->select();
		$select->join('users_new', 'buys.user_id=users_new.user_id',array('*'),'left');
		$select->join('productsnew', 'buys.product_id=productsnew.product_id',array('*'),'left');
		$select->where('buys.buy_id="'.$bid.'"');
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet->current();	 
	}
	public function user_details($bid){
		$select = $this->tableGateway->getSql()->select();
		$select->join('users_new', 'buys.user_id=users_new.user_id',array('*'),'left');
		$select->where('buys.buy_id="'.$bid.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->current();	 
	}
    public function getallbuyProducts($user_id,$categoryid){
		$select = $this->tableGateway->getSql()->select();
		$select->join('productsnew', 'buys.product_id=productsnew.product_id',array('*'),'left');		
		if($categoryid!='all'){
			$select->where('buys.category_id="'.$categoryid.'"');
		}
		$select->where('buys.user_id="'.$user_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet;	 
	}
	public function deletebuyProduct($delete){
	   $deleteid=$this->tableGateway->delete(array('(buy_id IN ('.$delete.'))'));
		return $deleteid;	
	}
	public function buyproduct($pid){
	    $select = $this->tableGateway->getSql()->select();
		$select->join('users_new', 'buys.user_id=users_new.user_id',array('*'),'left');
		$select->join('productsnew', 'buys.product_id=productsnew.product_id',array('genericname'),'left');
		$select->where('buys.product_id="'.$pid.'"');
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet->current();	 
	}
	public function getbuyingDetails($buyData) {
	    $select = $this->tableGateway->getSql()->select();
		$select->join('productsnew', 'buys.product_id=productsnew.product_id',array('*'),'left');
		$select->join('users_new', 'buys.user_id=users_new.user_id',array('*'),'left');
		$select->where('buys.product_id="'.$buyData.'"');		
        $resultSet = $this->tableGateway->selectWith($select);
	    return $resultSet;	 
	}
    public function getbuyinglist($product_id) {
		$select = $this->tableGateway->getSql()->select();
		$select->join('productsnew', 'buys.product_id=productsnew.product_id',array('*'),'left');
		$select->join('users_new', 'buys.user_id=users_new.user_id',array('*'),'left');
		$select->where('buys.product_id="'.$product_id.'"');		
        $resultSet = $this->tableGateway->selectWith($select);
	    return $resultSet->current();	 
	}
	public function buyingDetails($buyId){
	    $select = $this->tableGateway->getSql()->select();
		$select->join('users_new', 'buys.user_id=users_new.user_id',array('email'),'left');
		$select->where('buys.buy_id="'.$buyId.'"');		
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->current();	 
	} 
	public function statusOnBuy($buyDetais){
	    if($buyDetais['status']='accepted'){
			$status=1;
		}else if($buyDetais['status']='pending'){
			$status=0;
		}
		$data = array(
			'order_status' 	     => $status,
		);
		$row=$this->tableGateway->update($data, array('buy_id' => $buyDetais['hid_buy_id']));				
		return $row;	
	}
	public function changeStatusbuyProduct($buy_id,$newStatus){
		$data = array(
			'order_status' 	     => $newStatus,
		);
		$row=$this->tableGateway->update($data, array('buy_id' =>$buy_id));				
		return $row;	
	}
	public function getDetailesOfProduct($buyId) {
	    $select = $this->tableGateway->getSql()->select();
		$select->join('productsnew', 'buys.product_id=productsnew.product_id',array('*'),'left');
		$select->join('users_new', 'buys.user_id=users_new.user_id',array('*'),'left');
		$select->join('history', 'buys.buy_id=history.id',array('*'),'left');
		$select->where('buys.buy_id="'.$buyId.'"');	
		$select->where('history.type="buy"');			
        $resultSet = $this->tableGateway->selectWith($select);
	    return $resultSet;	 
	}
	public function allbuyProducts(){
		$select = $this->tableGateway->getSql()->select();
		$select->join('productsnew', 'buys.product_id=productsnew.product_id',array('*'),'left');
		$resultSet = $this->tableGateway->selectWith($select);
	    return $resultSet;
	}
	public function alladminuserbuyproducts($user_id){
		$select = $this->tableGateway->getSql()->select();
		$select->join('productsnew', 'buys.product_id=productsnew.product_id',array('*'),'left');
		$select->where('buys.user_id="'.$user_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
	    return $resultSet;
	}
	public function getalladminuserboughtproducts($all_userids){
		$userIds='';
		for($i=0;$i<count($all_userids);$i++) {
			$userIds.=$all_userids[$i] . ",";
		}
		$userIds = substr($userIds,0,(strlen($userIds)-1));
		$select = $this->tableGateway->getSql()->select();
		$select->join('productsnew', 'buys.product_id=productsnew.product_id',array('*'),'left');
		$select->where(array('(buys.user_id  IN ('.$userIds.'))'));
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
}